﻿#include "UpdateWidget.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QFileDialog>
#include <QApplication>
#include <QThread>
#include <QMessageBox>

#pragma execution_character_set("utf-8")

UpdateWidget::UpdateWidget(const QString& title, QWidget *parent)
    : QGroupBox{title, parent}
{
    initUpdateWorker();
    setupUI();
    initConnection();
}

void UpdateWidget::getUpdateFileInfo(QString &filePath, QString &fileUrl)
{
    filePath = ledtFilePath->text();
    fileUrl = ledtFileUrl->text();
}

void UpdateWidget::setUpdateFileInfo(const QString &filePath, const QString &fileUrl)
{
    ledtFilePath->setText(filePath);
    ledtFileUrl->setText(fileUrl);
}

void UpdateWidget::setupUI()
{
    pbtnSelectFile = new QPushButton(tr("选择更新的文件"));
    pbtnUpdateFile = new QPushButton(tr("更新"));
    ledtFilePath = new QLineEdit();
    ledtFileUrl = new QLineEdit();
    barUpdate = new QProgressBar();

    QVBoxLayout* vLayout = new QVBoxLayout();
    this->setLayout(vLayout);
    {
        QHBoxLayout* hLayout = new QHBoxLayout();
        vLayout->addLayout(hLayout);
        hLayout->addWidget(pbtnSelectFile);
        hLayout->addWidget(pbtnUpdateFile);
        hLayout->addWidget(barUpdate);
        // hLayout->addStretch();
    }
    {
        QHBoxLayout* hLayout = new QHBoxLayout();
        vLayout->addLayout(hLayout);
        hLayout->addWidget(new QLabel(tr("文件本地路径")));
        hLayout->addWidget(ledtFilePath);
    }
    {
        QHBoxLayout* hLayout = new QHBoxLayout();
        vLayout->addLayout(hLayout);
        hLayout->addWidget(new QLabel(tr("文件远程地址")));
        hLayout->addWidget(ledtFileUrl);
    }
}

void UpdateWidget::initConnection()
{
    connect(pbtnSelectFile, &QAbstractButton::clicked, this, [&](){
        QString filePath = QFileDialog::getOpenFileName(nullptr,
                                                        "select file",
                                                        QApplication::applicationDirPath());//打开打开文件对话框
        if(!filePath.isEmpty()){
            ledtFilePath->setText(filePath);
        }
    });
    connect(this, &UpdateWidget::sigUpdateFile, mUpdateWorker, &UpdateWorker::onUpdateFile);
    connect(pbtnUpdateFile, &QAbstractButton::clicked, this, [&](){
        pbtnUpdateFile->setEnabled(false);
        emit sigUpdateFile(ledtFileUrl->text(), ledtFilePath->text());
    });
    connect(mUpdateWorker, &UpdateWorker::sigDownloadProgress, this, [&](qint64 rev, qint64 total){
        barUpdate->setMaximum(total);
        barUpdate->setValue(rev);
    });
    connect(mUpdateWorker, &UpdateWorker::sigDownloadFinished, this, [&](QString filename, bool success){
        pbtnUpdateFile->setEnabled(true);
        QString showMessage = QString("%1更新%2").arg(QFileInfo(filename).fileName())
                                  .arg(success ? tr("成功\n请关闭软件后重新启动!") : tr("失败!"));
        QMessageBox::information(nullptr, tr("提示"), showMessage);
    });
}

void UpdateWidget::initUpdateWorker()
{
    mUpdateWorker = new UpdateWorker();
    mUpdateThr = new QThread();
    mUpdateWorker->moveToThread(mUpdateThr);
    connect(mUpdateWorker, &QObject::destroyed, mUpdateThr, &QThread::quit);
    connect(mUpdateThr, &QThread::finished, mUpdateThr, &QThread::deleteLater);
    connect(mUpdateThr, &QThread::started, mUpdateWorker, &UpdateWorker::onInit);
    connect(qApp, &QApplication::aboutToQuit, mUpdateWorker, &UpdateWorker::onQuit);
    mUpdateThr->start();
}


